﻿using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using LogWare.AppCode;
using LogWare.Model;
using LogWare.Properties;
using LogWare.Views;

namespace LogWare.ViewModel {
	public class OrderViewModel : EditableBaseViewModel {
		private string _name;
		private string _profession;
		private string _taxCode;
		private string _taxOffice;
		private string _address;
		private string _postalCode;
		private string _city;
		private decimal _totalCost;
		private decimal _totalTax;
		private decimal _totalPrice;
		private NotifiableCollection<OrderDetailVM> _lines;

		public OrderViewModel() {
			
			_lines = new NotifiableCollection<OrderDetailVM>();
			_lines.CollectionChanged += (s, e) => CalculateTotals();
			_lines.ItemChanged += (s, e) => CalculateTotals();
		}

		public NotifiableCollection<OrderDetailVM> Lines {
			get { return _lines; }
			set { 
				_lines = value;
				RaisePropertyChanged("Lines");
			}
		}
			
		[DisplayName(@"Επωνυμία")]
		[State]
		public string Name {
			get { return _name; }
			set {
				_name = value;
				RaisePropertyChanged("Name");
			}
		}

		[DisplayName(@"Επάγγελμα")]
		[State]
		public string Profession {
			get { return _profession; }
			set {
				_profession = value;
				RaisePropertyChanged("Profession");
			}
		}

		[DisplayName(@"Α.Φ.Μ.")]
		[State]
		public string TaxCode {
			get { return _taxCode; }
			set {
				_taxCode = value;
				RaisePropertyChanged("TaxCode");
			}
		}

		[DisplayName(@"Δ.Ο.Υ.")]
		[State]
		public string TaxOffice {
			get { return _taxOffice; }
			set {
				_taxOffice = value;
				RaisePropertyChanged("TaxOffice");
			}
		}

		[DisplayName(@"Διεύθυνση")]
		[State]
		public string Address {
			get { return _address; }
			set {
				_address = value;
				RaisePropertyChanged("Address");
			}
		}

		[DisplayName(@"T.K.")]
		[State]
		public string PostalCode {
			get { return _postalCode; }
			set {
				_postalCode = value;
				RaisePropertyChanged("PostalCode");
			}
		}

		[DisplayName(@"Πόλη")]
		[State]
		public string City {
			get { return _city; }
			set {
				_city = value;
				RaisePropertyChanged("City");
			}
		}

		[DisplayName(@"Αξία")]
		public decimal TotalCost {
			get { return _totalCost; }
			set {
				_totalCost = value;
				RaisePropertyChanged("TotalCost");
			}
		}

		[DisplayName(@"Φ.Π.Α.")]
		public decimal TotalTax {
			get { return _totalTax; }
			set {
				_totalTax = value;
				RaisePropertyChanged("TotalTax");
			}
		}

		[DisplayName(@"Συνολικό Ποσό")]
		public decimal TotalPrice {
			get {

				return _totalPrice;
			}
			set {
				_totalPrice = value;
				RaisePropertyChanged("TotalPrice");
			}
		}

		private void CalculateTotals() {
			IEnumerator<OrderDetailVM> lineEnum = _lines.GetEnumerator();
			_totalCost = 0;
			_totalTax = 0;
			_totalPrice = 0;
			while(lineEnum.MoveNext()) {
				_totalCost += lineEnum.Current.Price*lineEnum.Current.Quantity;
				_totalTax += lineEnum.Current.Price*lineEnum.Current.Quantity*lineEnum.Current.Vat*0.01m;
				_totalPrice += lineEnum.Current.LinePrice;
			}
			RaisePropertyChanged("TotalCost");
			RaisePropertyChanged("TotalTax");
			RaisePropertyChanged("TotalPrice");
		}
	}
}
